using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.ReportingServices.MDXQueryGenerator.ObjectModel;
using Microsoft.AnalysisServices.AdomdClient;
using RetailAnalytic.Core;
using Microsoft.AnalysisServices.Browse;
using System.Xml.Serialization;
using System.IO;

namespace RetailAnalytic.Map.Engine
{
    public partial class UCOther : UserControl
    {
        private Filter filterPresentation;
        private PresentationMap presentationMap;
        public UCOther()
        {
            InitializeComponent();
        }

        public void LoadFilter(PresentationMap presentationMap)
        {
            this.presentationMap = presentationMap;
            
            AdomdConnection _adomdcn;
            try
            {
                if (presentationMap.XMLFilterData != "")
                {
                    this.filterPresentation = RetailAnalytic.Core.Utils.DeSerializeFilter(presentationMap.XMLFilterData);
                    _adomdcn = new AdomdConnection();
                    _adomdcn.ConnectionString = presentationMap.DataSource;
                    _adomdcn.Open();
                    this.filterOLAP.InitializeWithObject(_adomdcn, presentationMap.cubeName, filterPresentation);
                    this.filterOLAP.Visible = true;
                }
                else
                {
                    this.filterOLAP.Visible = false;
                }
            }
            catch (Exception er)
            {

                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY, 
                    Environment.StackTrace, er));
            }
        }
        public void SaveFiltr()
        {
            try
            {
                this.filterPresentation = filterOLAP.GetFilterObject();
                this.presentationMap.XMLFilterData = Utils.SerializeFilter(this.filterPresentation);
            }
            catch (Exception er)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY, 
                    Environment.StackTrace, er));
            }
        }

        public void LoadConfig()
        {
            this.propertyGrid1.SelectedObject = InitMapConfig();
        }
        
        private MapConfig InitMapConfig()
        {
            MapConfig _mapConfig = new MapConfig();
            string _tempFile = Application.StartupPath + "\\tempCONF.xml";
            if (File.Exists(_tempFile))
            {
                File.Delete(_tempFile);
            }

            FileStream myFileStream = null;
            try
            {
                string _xMLConfiguration = presentationMap.XMLConfiguration;
                File.WriteAllText(_tempFile, _xMLConfiguration);
                XmlSerializer xmlo = new XmlSerializer(typeof(MapConfig));
                myFileStream = new FileStream(_tempFile, FileMode.Open);
                if (_xMLConfiguration != "")
                {
                    _mapConfig = (MapConfig)xmlo.Deserialize(myFileStream);
                }
            }
            catch (Exception er)
            {
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY,
                    Environment.StackTrace, er));
            }
            finally
            {
                myFileStream.Close();
                File.Delete(_tempFile);
            }
            return _mapConfig;
        }
        public void SaveConfig()
        {
            string _tempFile = Application.StartupPath + "\\tempCONF.xml";
            TextWriter tw = null;
            try
            {
                if (File.Exists(_tempFile))
                {
                    File.Delete(_tempFile);
                }
                tw = new StreamWriter(_tempFile, false);
                XmlSerializer xmlo = new XmlSerializer(typeof(MapConfig));
                xmlo.Serialize(tw, this.propertyGrid1.SelectedObject);
                tw.Close();
                presentationMap.XMLConfiguration = File.ReadAllText(_tempFile, Encoding.UTF8);

            }
            catch (Exception er)
            {
                tw.Close();
                Dialog.Show(Dialog_type.Error, SR.StringLibrary.GetStrMSG(SR.StringLibrary.KeyEnum.MESSAGE_EMPTY,
                    Environment.StackTrace, er));
            }
            finally
            {
                File.Delete(_tempFile);
            }
        }
    }
}
